import wx
import popbtn
import rememberlistwnd
import rememberfloatwnd
import remembertaskbar

class RemberSearchWnd(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = (~(wx.RESIZE_BORDER)) &( wx.FULL_REPAINT_ON_RESIZE | wx.SIMPLE_BORDER |wx.FRAME_NO_TASKBAR| wx.STAY_ON_TOP |wx.FRAME_SHAPED)
        wx.Frame.__init__(self, *args, **kwds)
        
        self.bmp = wx.BitmapFromImage(wx.Image('pic/float.png',wx.BITMAP_TYPE_ANY))
        self.SetClientSize((self.bmp.GetWidth(), self.bmp.GetHeight()))
        self.CentreOnScreen(wx.HORIZONTAL)
        
        self.ListWindowID = 112
        self.rememberTaskBar = remembertaskbar.RememberTaskBar(self)
        self.RefRememberFloatWnd = [ ]
    
        self.hSizer = wx.BoxSizer(wx.HORIZONTAL)      
        self.hSizer.Add(self.CreateSearchBtn(self),1,wx.CENTER|wx.LEFT|wx.RIGHT,10)
        self.hSizer.Add(self.CreateEditBtn(self),0,wx.CENTER|wx.RIGHT,10)
        
        self.vSizer = wx.BoxSizer(wx.VERTICAL)  
        self.vSizer.Add(self.hSizer,0,wx.EXPAND|wx.TOP,15)
        self.SetSizer(self.vSizer)
        self.Layout()
        
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnFrameLeftDown)
        self.Bind(wx.EVT_LEFT_UP, self.OnFrameLeftUp)
        self.Bind(wx.EVT_MOTION, self.OnFrameMotion)
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        
    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        dc.DrawBitmap(self.bmp, 0,0, True)
    def OnClose(self, event):
        for wnd in self.RefRememberFloatWnd:
            wnd.Close()
        event.Skip()
        
    def CreateSearchBtn(self,parent):
        self.serchBtn = wx.SearchCtrl(parent, style=wx.TE_PROCESS_ENTER)
        self.serchBtn.Bind(wx.EVT_TEXT, self.OnSearch)
        self.serchBtn.Bind(wx.EVT_TEXT_ENTER, self.OnSearch)
        return self.serchBtn
    def OnSearch(self,event):
        filtertext = self.serchBtn.GetValue()
        ListWnd = wx.FindWindowById(self.ListWindowID)
        if "" == filtertext :
            if None != ListWnd:  ListWnd.Close()
            return
        if None == ListWnd: # if not exsit create it
            SelfPos = self.GetPosition() 
            ListWndPos = (SelfPos.x, SelfPos.y + self.GetSize().y) #get the listwnd position
            ListWndSize = (self.GetSize().x, 400) # get the listwnd size
            ListWnd = rememberlistwnd.RememberListWnd(self, self.ListWindowID, " ",size =ListWndSize, pos = ListWndPos)
            ListWnd.Show()
        
        ListWnd.ReQuery(filtertext) # search the tag 
        self.serchBtn.SetFocus()
        
    def CreateEditBtn(self,parent):
        self.editbtn = popbtn.PopBtn(parent,-1,"pic/x1.png","pic/x2.png","test tip")
        self.editbtn.AddManu("New",self.OnNew)
        self.editbtn.AddManu("Hide",self.OnHide)
        return self.editbtn
    def OnHide(self,event):
        ListWnd = wx.FindWindowById(self.ListWindowID)
        if None != ListWnd: ListWnd.Close()
        self.Hide()
    def OnNew(self, event):
        floatWnd = rememberfloatwnd.RememberFloatWnd(self, -1, "", NoteID = None) # create new remember with none noteid
        self.AddRememberFloatWndRef(floatWnd)
        floatWnd.Show()
    def AddRememberFloatWndRef(self, wnd):
        self.RefRememberFloatWnd.append(wnd)
    def RemoveRememberFloatWndRef(self, wnd):
        self.RefRememberFloatWnd.remove(wnd)
    
    def SetWindowShape(self, evt=None):
        r = wx.RegionFromBitmap(self.bmp)
        self.hasShape = self.SetShape(r)
    def OnFrameLeftDown(self, event):
        if event.EventObject != self: return
        self.CaptureMouse()
        mouse=wx.GetMousePosition()
        frame=self.GetPosition()
        self.delta=wx.Point(mouse.x-frame.x,mouse.y-frame.y)
    def OnFrameMotion(self, event):      
        if event.EventObject != self: return
        if event.Dragging() and event.LeftIsDown():
             mouse=wx.GetMousePosition()
             self.Move((mouse.x-self.delta.x,\
                         mouse.y-self.delta.y))
             ListWnd = wx.FindWindowById(self.ListWindowID)
             if None != ListWnd:
                 SelfWinPos = self.GetPosition()
                 ListWndPos = (SelfWinPos.x, SelfWinPos.y+self.GetSize().y)
                 ListWnd.SetPosition(ListWndPos)
    def OnFrameLeftUp(self,event):
        if event.EventObject != self: return
        if self.HasCapture():
             self.ReleaseMouse()
    
    
    

if __name__ == "__main__":
    app = wx.PySimpleApp()
    wx.InitAllImageHandlers()
    main = RemberSearchWnd(None, -1, "Notes")
    app.SetTopWindow(main)
    main.Show()
    app.MainLoop()
